package com.ch.base.controller;

import com.ch.base.enums.CodeEnum;
import com.ch.base.model.ChBasicOrderModel;
import com.ch.base.result.Result;
import com.ch.base.service.CommonService;
import com.ch.base.service.OrderService;
import com.ch.base.utils.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * created in 2019-04-19 10:43
 **/
@RestController
@RequestMapping("/api/erp/v1/order")
@Slf4j
public class OrderController {

    @Resource
    private CommonService commonService;
    @Resource
    private OrderService orderService;

    @PostMapping("list")
    public Result<?> list(@RequestBody ChBasicOrderModel request) {
        //分页判断
        if (commonService.paginationSupport(request.getPageIndex(), request.getPageSize())) {
            return orderService.list(request);
        } else {
            return Result.create(CodeEnum.FAIL.getCode(), "参数类型不匹配");
        }
    }

    @PostMapping("upload")
    public Result<?> upload(@RequestParam MultipartFile file) {

        if (Objects.isNull(file) || file.getSize() == 0) {
            return Result.create(CodeEnum.FAIL.getCode(), CodeEnum.FILE_UPLOAD_NULL.getMsg());
        }
        if (FileUtil.isNotXlSX(Objects.requireNonNull(file.getOriginalFilename()))) {
            return Result.create(CodeEnum.FAIL.getCode(), CodeEnum.FILE_FORMAT_ERROR.getMsg());
        }
        return orderService.upload(file);
    }
}